//
//  TestElementsTableViewController.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2023/7/11.
//  Copyright © 2023 moon. All rights reserved.
//

#import "TestElementsTableViewController.h"
#import "Menu.h"
@interface TestElementsTableViewController ()

@end

@implementation TestElementsTableViewController2

+ (MKTFuncItem *)funcItem{
    return [MKTFuncItem funcWithName:@"ElementTablePlain表单" viewControllerClass:self];
}
+ (UITableViewStyle)tableViewStyle{
    UITableViewStyle style = UITableViewStylePlain;
    return style;
}
@end

@interface TestElementsTableViewController()
@property(nonatomic,strong) UITapGestureRecognizer *tapGesture;
@end
@implementation TestElementsTableViewController

+ (UITableViewStyle)tableViewStyle{
    UITableViewStyle style = UITableViewStylePlain;
    if (@available(iOS 13.0, *)) {
        style = UITableViewStyleInsetGrouped;
    }
    return style;
}
+ (MKTFuncItem *)funcItem{
    return [MKTFuncItem funcWithName:@"ElementTableGroup表单" viewControllerClass:self];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    MKUITableViewModel *model = self.model;
    [model removeAllSectionModels];
    [self _testElementDetail];
    [self _testElementImage];
    [self _testSwitchElements];
    [self _testElementSelect];
    [self _testElementTextInput];
    [self _testElementButton];
    [model reloadTableViewData];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(__onKeyboardShow:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(__onKeyboardChange:) name:UIKeyboardDidChangeFrameNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(__onKeyboardHide:) name:UIKeyboardDidHideNotification object:nil];
    //
    self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(__onTap:)];
    self.tapGesture.enabled = NO;
    [self.view addGestureRecognizer:self.tapGesture];
}
- (void)__onTap:(id)sender{
    [self.view endEditing:YES];
}
- (void)__onKeyboardShow:(NSNotification *)noti{
    self.tapGesture.enabled = YES;
    [self.tableView mk_adjustContentWithUIKeyboardDidShowNotification:noti responderViewClass:[UITableViewCell class] contentInsets:UIEdgeInsetsZero window:UIApplication.sharedApplication.keyWindow];
}
- (void)__onKeyboardChange:(NSNotification *)noti{
    [self.tableView mk_adjustContentWithUIKeyboardDidShowNotification:noti responderViewClass:[UITableViewCell class] contentInsets:UIEdgeInsetsZero window:UIApplication.sharedApplication.keyWindow];
}
- (void)__onKeyboardHide:(NSNotification *)noti{
    self.tapGesture.enabled = NO;
    UIEdgeInsets insets = self.tableView.contentInset;
    insets.bottom = 0;
    self.tableView.contentInset = insets;
}
- (void)_testElementDetail{
    [self.model addSectionModel:[[MKUITableViewSectionModel alloc] initWithBlankHeadView:20]];
    {
        MKUIElementTableViewCellModelDetail *cm = [MKUIElementTableViewCellModelDetail modelWithValue:nil cellClass:[MKUIElementTableViewCellDetail class] whenClick:^(MKUIElementTableViewCellModelDetail *cm) {
            cm.el_detailText = [NSString stringWithFormat:@"%@详细内容1",cm.el_detailText];
            cm.el_accessoryType = cm.el_accessoryType==UITableViewCellAccessoryDisclosureIndicator?UITableViewCellAccessoryNone:UITableViewCellAccessoryDisclosureIndicator;
            cm.el_subTitleText = cm.el_subTitleText.length>0?nil:@"子标题1";
            [cm setSelected:NO animated:NO];
            if(UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero, cm.el_iconImageMarggin)){
                cm.el_iconImageSize = CGSizeMake(60, 30);
                cm.el_iconImageMarggin = UIEdgeInsetsMake(10, 10, 10, 10);
            }else{
                cm.el_iconImageSize = CGSizeMake(28, 28);
                cm.el_iconImageMarggin = UIEdgeInsetsZero;
            }
            [cm refresh];
        }];
        cm.el_accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cm.el_iconImage = [Menu randomImage];
        cm.el_titleText = @"标题1";
        cm.el_subTitleText = @"子标题1";
        cm.el_detailText = @"详细内容1";
        [self.model addCellModel:cm];
    }
    {
        MKUIElementTableViewCellModelDetail *cm = [MKUIElementTableViewCellModelDetail modelWithValue:nil cellClass:[MKUIElementTableViewCellDetail class] whenClick:^(MKUIElementTableViewCellModelDetail *cm) {
            cm.el_detailText = [NSString stringWithFormat:@"%@详细内容1",cm.el_detailText];
            cm.el_accessoryType = cm.el_accessoryType==UITableViewCellAccessoryCheckmark?UITableViewCellAccessoryNone:UITableViewCellAccessoryCheckmark;
            [cm setSelected:NO animated:YES];
        }];
        cm.el_accessoryType = UITableViewCellAccessoryCheckmark;
        cm.el_iconImage = [Menu randomImage];
        cm.el_titleText = @"标题1";
        cm.el_subTitleText = @"子标题1";
        cm.el_detailText = @"详细内容1";
        [self.model addCellModel:cm];
    }
}
- (void)_testSwitchElements{
    [self.model addSectionModel:[[MKUITableViewSectionModel alloc] initWithBlankHeadView:20]];
    if (@available(iOS 13.0, *)){
        MKUIElementTableViewCellModelSwitch *cm = [MKUIElementTableViewCellModelSwitch modelWithValue:nil cellClass:[MKUIElementTableViewCellSwitch class] whenClick:nil];
        @MK_WEAKIFY(self);
        cm.el_whenSwitchChange = ^(MKUIElementTableViewCellModelSwitch * _Nonnull cm, BOOL on) {
            @MK_NORMALIZE(self);
            self.overrideUserInterfaceStyle = on?UIUserInterfaceStyleDark:UIUserInterfaceStyleLight;
        };
        cm.el_titleText = @"是否黑暗模式";
        cm.el_on = self.traitCollection.userInterfaceStyle==UIUserInterfaceStyleDark;
        [self.model addCellModel:cm];
    }
    {
        MKUIElementTableViewCellModelSwitch *cm = [MKUIElementTableViewCellModelSwitch modelWithValue:nil cellClass:[MKUIElementTableViewCellSwitch class] whenClick:nil];
        cm.el_whenSwitchChange = ^(MKUIElementTableViewCellModelSwitch * _Nonnull cm, BOOL on) {
            NSLog(@"on:%@",@(cm.el_on));
        };
        cm.el_iconImage = [Menu randomImage];
        cm.el_titleText = @"Switch标题1";
        [self.model addCellModel:cm];
    }
    {
        MKUIElementTableViewCellModelSwitch *cm = [MKUIElementTableViewCellModelSwitch modelWithValue:nil cellClass:[MKUIElementTableViewCellSwitch class] whenClick:nil];
        cm.el_whenSwitchChange = ^(MKUIElementTableViewCellModelSwitch * _Nonnull cm, BOOL on) {
            cm.el_hiddenEmptyIconImage = cm.el_on;
            cm.el_subTitleText = cm.el_on?@"Switch选中":@"Switch未选中";
            [cm refresh];
        };
        cm.el_iconImage = nil;
        cm.el_titleText = @"Switch标题1";
        cm.el_on = YES;
        [self.model addCellModel:cm];
    }
}
- (void)_testElementButton{
    [self.model addSectionModel:[[MKUITableViewSectionModel alloc] initWithBlankHeadView:20]];
    {
        MKUIElementTableViewCellModelButton *cm = [MKUIElementTableViewCellModelButton modelWithValue:nil cellClass:[MKUIElementTableViewCellButton class] whenClick:^(MKUIElementTableViewCellModelButton *cm) {
            cm.el_titleText = [NSString stringWithFormat:@"%@Button标题1",cm.el_titleText];
            [cm setSelected:NO animated:YES];
        }];
        cm.el_iconImage = [Menu randomImage];
        cm.el_titleText = @"Button标题1";
        [self.model addCellModel:cm];
    }
    {
        MKUIElementTableViewCellModelButton *cm = [MKUIElementTableViewCellModelButton modelWithValue:nil cellClass:[MKUIElementTableViewCellButton class] whenClick:^(MKUIElementTableViewCellModelButton *cm) {
            cm.el_buttonStyle = (cm.el_buttonStyle+1)%3;
            [cm setSelected:NO animated:YES];
        }];
        cm.el_titleText = @"Button标题1";
        [self.model addCellModel:cm];
    }
}

- (void)_testElementSelect{
    [self.model addSectionModel:[[MKUITableViewSectionModel alloc] initWithBlankHeadView:20]];
    
    NSArray<MKUIElementSelectOption *> *options = [MKUIElementSelectOption optionsWithTexts:@[
        @"选项1",
        @"选项2",
        @"选项3",
        @"选项4",
    ]];
    {
        MKUIElementTableViewCellModelSelection *cm = [MKUIElementTableViewCellModelSelection modelWithValue:nil cellClass:[MKUIElementTableViewCellSelection class] whenClick:nil];
        cm.el_titleText = @"单选1";
        cm.el_allowsMultipleSelection = NO;
        cm.el_options = options;
        cm.el_doneButtonText = @"完成";
        cm.el_selectedValue = options.lastObject.value;
        cm.el_whenSelectionDone = ^(__kindof MKUIElementTableViewCellModelSelection * _Nonnull cm) {
            NSLog(@"selected:%@",cm.el_selectedValue);
            [cm refresh];
        };
        [self.model addCellModel:cm];
    }
    {
        MKUIElementTableViewCellModelSelection *cm = [MKUIElementTableViewCellModelSelection modelWithValue:nil cellClass:[MKUIElementTableViewCellSelection class] whenClick:nil];
        cm.el_titleText = @"多选1";
        cm.el_allowsMultipleSelection = YES;
        cm.el_options = options;
        cm.el_detailTextWhenNoSelected = @"未选择";
        cm.el_selectedValues = @[@"选项1",@"选项2"];
        cm.el_whenSelectionDone = ^(__kindof MKUIElementTableViewCellModelSelection * _Nonnull cm) {
            NSLog(@"selected:%@",[cm.el_selectedValues componentsJoinedByString:@","]);
            [cm refresh];
        };
        [self.model addCellModel:cm];
    }
}
- (void)_testElementTextInput{
    [self.model addSectionModel:[[MKUITableViewSectionModel alloc] initWithBlankHeadView:20]];
    {
        MKUIElementTableViewCellModelTextInput *cm = [MKUIElementTableViewCellModelTextInput modelWithValue:nil cellClass:[MKUIElementTableViewCellTextInput class] whenClick:nil];
        cm.el_iconImage = [Menu randomImage];
        cm.el_titleText = @"输入单行文本";
        cm.el_placeholder = @"请输入";
        cm.el_allowsMultipleLineText = NO;
//        @MK_WEAKIFY(self);
        cm.el_whenChange = ^NSString * _Nonnull(__kindof MKUIElementTableViewCellModelTextInput * _Nonnull cm, NSString * _Nonnull text) {
//            @MK_NORMALIZE(self);
            if(text.length>20){
                return [text substringToIndex:20];
            }
            return text;
        };
        cm.el_whenDone = ^(__kindof MKUIElementTableViewCellModelTextInput * _Nonnull cm) {
            NSLog(@"done:%@",cm.el_text);
            [cm refresh];
        };
        [self.model addCellModel:cm];
    }
    {
        MKUIElementTableViewCellModelTextInput *cm = [MKUIElementTableViewCellModelTextInput modelWithValue:nil cellClass:[MKUIElementTableViewCellTextInput class] whenClick:nil];
        cm.el_iconImage = [Menu randomImage];
        cm.el_titleText = @"输入多行文本";
        cm.el_placeholder = @"请输入";
        cm.el_allowsMultipleLineText = YES;
        cm.el_doneButtonText = @"完成";
//        @MK_WEAKIFY(self);
        cm.el_whenChange = ^NSString * _Nonnull(__kindof MKUIElementTableViewCellModelTextInput * _Nonnull cm, NSString * _Nonnull text) {
//            @MK_NORMALIZE(self);
            NSLog(@"change:%@",text);
            return text;
        };
        cm.el_whenDone = ^(__kindof MKUIElementTableViewCellModelTextInput * _Nonnull cm) {
            NSLog(@"done:%@",cm.el_text);
            [cm refresh];
        };
        [self.model addCellModel:cm];
    }
}
- (void)_testElementImage{
    [self.model addSectionModel:[[MKUITableViewSectionModel alloc] initWithBlankHeadView:20]];
    {
        MKUIElementTableViewCellModelImage *cm = [MKUIElementTableViewCellModelImage modelWithValue:nil cellClass:[MKUIElementTableViewCellImage class] whenClick:^(MKUIElementTableViewCellModelImage *cm) {
            cm.el_subTitleText = [NSString stringWithFormat:@"%@子标题1",cm.el_subTitleText];
            if(UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero, cm.el_detailImageMarggin)){
                cm.el_detailImageSize = CGSizeMake(40, 40);
                cm.el_detailImageMarggin = UIEdgeInsetsMake(0, 5, 0, 5);
            }else{
                cm.el_detailImageSize = CGSizeMake(28, 28);
                cm.el_detailImageMarggin = UIEdgeInsetsZero;
            }
            [cm refresh];
        }];
        cm.el_titleText = @"图片";
        cm.el_subTitleText = @"子标题1";
        cm.el_detailImage = [Menu randomImage];
        [self.model addCellModel:cm];
    }
    {
        MKUIElementTableViewCellModelImage *cm = [MKUIElementTableViewCellModelImage modelWithValue:nil cellClass:[MKUIElementTableViewCellImage class] whenClick:^(MKUIElementTableViewCellModelImage *cm) {
            cm.el_subTitleText = [NSString stringWithFormat:@"%@子标题2",cm.el_subTitleText];
            cm.el_detailImage = cm.el_detailImage?nil:[Menu randomImage];
            [cm refresh];
        }];
        cm.el_titleText = @"图片2";
        cm.el_subTitleText = @"子标题2";
        cm.el_detailImage = [Menu randomImage];
        [self.model addCellModel:cm];
    }
}
@end
